/*
    AuthorNote: ***
    Created By: Bambom Date:DATE
*/

using System;
using AutoChess;
using GameBase;
using UnityEngine;
using Yunchang;

namespace GameUnity
{
    /*将Unity接口和特性隔离在此处*/
    public class ResourcesImp:IResources
    {
        public void UnloadAsset(string resPath, int count = 1)
        {
            AssetManager.Instance.UnloadAsset(resPath, count);
        }

        /// <summary>
        /// 返回一个
        /// </summary>
        public GameObject AllocGameObject(string resPath)
        {
            GameObject dummyPoint = new GameObject();
            AssetManager.Instance.LoadAssetAsync(resPath, (asset,
                args) =>
            {
                if (asset == null)
                {
                    BLogger.EditorFatal("asset: {0} ,未能加载!",resPath);
                    return;
                }

                var obj = GameObject.Instantiate(asset, dummyPoint.transform) as GameObject;
            }, null);

            return dummyPoint;
        }

        public void AllocGameObjectAsync(string resPath, Action<GameObject> onLoaded)
        {
            AssetManager.Instance.LoadAssetAsync(resPath, (asset,
                args) =>
            {
                if (asset == null)
                {
                    BLogger.EditorFatal("asset: {0} ,未能加载!",resPath);
                    return;
                }

                var obj = GameObject.Instantiate(asset) as GameObject;
                if (obj != null)
                {
                    onLoaded(obj);
                }
            }, null);
        }

        public T LoadResourcesAsset<T>(string resPath) where T: UnityEngine.Object
        {
            return Resources.Load<T>(resPath);
        }
    }
}